package com.topsmob.processer;

import com.jayway.jsonpath.InvalidPathException;
import com.topsmob.piepline.JeeCmsPiePline;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.JsonPathSelector;

import java.util.ArrayList;
import java.util.List;

/**
 * 今日头条爬虫
 * Created by lyh on 2017/6/17.
 */
public class TouTiaoProcesser implements PageProcessor{
    public void process(Page page) {
        if(page.getUrl().toString().contains("category=news_society")){
            String nextmaxTime = new JsonPathSelector("$.next[*].max_behot_time").select(page.getRawText());
            if(nextmaxTime != null){
                String nextPage = "http://www.toutiao.com/api/pc/feed/?category=news_society&utm_source=toutiao&widen=1&max_behot_time="+nextmaxTime
                        +"&max_behot_time_tmp=0&tadrequire=true&as=A10579A46420CB7&cp=594470EC6BE7AE1";
                page.addTargetRequest(new Request(nextPage));
                //获得文章页码
                List<String> pageList = new JsonPathSelector("$.data").selectList(page.getRawText());
                //List<String> tagList = new JsonPathSelector("$.data[*].chinese_tag").selectList(page.getRawText());
                List<String> urlPagelist = new ArrayList<String>();

                for(String str:pageList){
                    try{
                        JsonPathSelector jsonPathSelector = new JsonPathSelector("$.chinese_tag");
                        String tag = new JsonPathSelector("$.chinese_tag").select(str);
                        String groupId = new JsonPathSelector("$.group_id").select(str);
                        if(tag!=null && tag.equals("社会")){
                            urlPagelist.add("http://www.toutiao.com/a"+groupId);
                        }
                    }catch (InvalidPathException iv){
                        //没有标签的忽略掉
                    }
                }
                page.addTargetRequests(urlPagelist);
                page.setSkip(true);
            }

        }else{
           // System.out.println(page.getUrl().toString());
            String title = page.getHtml().xpath("//h1[@class=\"article-title\"]/text()").toString();
            //System.out.println("标题："+title);
            page.putField("title",title);
            List<String> label = page.getHtml().xpath("//ul[@class=\"label-list\"]/li/a/text()").all();
            page.putField("label",label);
            String content = page.getHtml().xpath("//div[@class=\"article-content\"]").toString();
            //System.out.println("内容:"+content);
            page.putField("content",content);
            page.putField("postId",page.getUrl().replace("http://www.toutiao.com/a",""));
        }
    }

    public Site getSite() {
       return  Site.me().setRetryTimes(1).setTimeOut(30*1000).setDomain("http://www.toutiao.com/").setCharset("utf-8");
    }

    public static void main(String[] args) {
       Spider spider =  Spider.create(new TouTiaoProcesser());
       spider.addPipeline(new JeeCmsPiePline(spider));
       spider.addUrl("http://www.toutiao.com/api/pc/feed/?category=news_society&utm_source=toutiao&widen=1&max_behot_time=0&max_behot_time_tmp=0&tadrequire=true&as=A10579A46420CB7&cp=594470EC6BE7AE1");
       spider.thread(5).run();

    }
}
